package cn.sc.allinpay.allfavour.base.widget;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * 自定义RecyclerView 子控件间距 gridView形式
 *
 * @author panyi
 */
public class DecorationGrid extends RecyclerView.ItemDecoration {
    private int horSpace;
    private int verSpace;

    public DecorationGrid(Context context, int horSpaceId, int vertSpaceId) {
        horSpace = context.getResources().getDimensionPixelSize(horSpaceId);
        verSpace = context.getResources().getDimensionPixelSize(vertSpaceId);
    }

    @SuppressWarnings("deprecation")
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                               RecyclerView.State state) {

        int postion = parent.getChildPosition(view);
        if (postion % 2 == 0) {
            outRect.left = horSpace;
            outRect.right = horSpace / 2;
        } else {
            outRect.left = horSpace / 2;
            outRect.right = horSpace;
        }

        outRect.bottom = verSpace;
        // Add top margin only for the first item to avoid double space
        // between items
        if (postion == 0 || postion == 1 || postion == 2)
            outRect.top = verSpace;
    }
}
